package com.ibiaoke.boss.controller;

import com.ibiaoke.boss.service.LoginService;
import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.common.utils.SmsUtil;
import com.ibiaoke.customer.entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * Created by biaoke on 2017/6/23.
 */
@Controller
public class WxSmsController extends BaseController {

    private static Random r = new Random();
    private static char[] chs = "0123456789".toCharArray();
    private static final int NUMBER_OF_CHS = 4;
    private static final int IMG_WIDTH = 65;
    private static final int IMG_HEIGHT = 25;

    @Autowired
    private LoginService loginService;

    @RequestMapping("/sendMobileVerifyCode")
    @ResponseBody
    public Res sendVerifyCode(String mobile,String userPicCode,HttpServletRequest request) {
        String sysPicCode =  request.getSession().getAttribute("piccode").toString();
        AssertUtils.requireEquals(sysPicCode,userPicCode,"验证不正确，请重新输入");
        SmsUtil.sendVerifyCode(mobile);
        return Res.create();
    }

    @RequestMapping("/sendMyVerifyCode")
    @ResponseBody
    public Res sendMyVerifyCode() {
        Customer customer = getLoginUser();
        String mobile = customer.getCustomerMobile();
        SmsUtil.sendVerifyCode(mobile);
        return Res.create();
    }

    @RequestMapping("/mobileFastLanding")
    @ResponseBody
    public Res mobileFastLanding(String mobile) {
        SmsUtil.sendVerifyCode(mobile);
        return Res.create();
    }

    @RequestMapping("/getPicCode")
    @ResponseBody
    public void getPicCode(HttpServletRequest request, HttpServletResponse response)throws Exception{
        // 实例化BufferedImage
        BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        // 验证码图片的背景颜色
        Color c = new Color(200, 200, 255);
        g.setColor(c);
        // 图片的边框
        g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
        // 用于保存验证码字符串
        StringBuffer sb = new StringBuffer();
        // 数组的下标
        int index;
        for (int i = 0; i < NUMBER_OF_CHS; i++) {
            // 随机一个下标
            index = r.nextInt(chs.length);
            // 随机一个颜色
            g.setColor(new Color(r.nextInt(88), r.nextInt(210), r.nextInt(150)));
            // 画出字符
            g.drawString(chs[index] + "", 15 * i + 3, 18);
            // 验证码字符串
            sb.append(chs[index]);
        }
        // 将验证码字符串保存到session中
        request.getSession().setAttribute("piccode", sb.toString());
        // 向页面输出图像
        ImageIO.write(image, "jpg", response.getOutputStream());
    }
}
